//
//  ZSY_AllStoryListViewController.m
//  ZSY_BreadTrip
//
//  Created by 张诗宇 on 16/1/4.
//  Copyright © 2016年 张诗宇. All rights reserved.
//

#import "ZSY_AllStoryListViewController.h"
#import "ZSY_AllStoryListCollectionViewCell.h"
#import "ZSY_AllStoryModel.h"
#import "ZSY_HomeDetailViewController.h"

@interface ZSY_AllStoryListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) ZSY_AllStoryListCollectionViewCell *cell;
@property (nonatomic, retain) ZSY_CollectionView *allStoryCollectionView;
@property (nonatomic, retain) NSMutableArray *allStoryArray;

@property(nonatomic,assign)NSInteger nextPage;//记录下一页
@property(nonatomic,assign)BOOL isUpLoading;//标记上拉操作还是下拉操作，yes为上拉


@end

@implementation ZSY_AllStoryListViewController

- (void)dealloc {
    [_allStoryArray release];
    [_cell release];
    [_flowLayout release];
    [_allStoryCollectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"精选故事";
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_new_poi_back_btn"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    self.allStoryArray = [NSMutableArray array];
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake((WIDTH - 30) / 2, 200);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    CGRect frame = self.view.frame;
    frame.size.height -= 114;
    self.allStoryCollectionView = [[ZSY_CollectionView alloc] initWithFrame:frame collectionViewLayout:_flowLayout];
    self.allStoryCollectionView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:243 / 255.0 blue:233 / 255.0 alpha:0];
    self.allStoryCollectionView.delegate = self;
    self.allStoryCollectionView.dataSource = self;
    [self.view addSubview:_allStoryCollectionView];
    [self.allStoryCollectionView registerClass:[ZSY_AllStoryListCollectionViewCell class] forCellWithReuseIdentifier:@"allStory"];
    self.isUpLoading = NO;
    
    // 2.集成上拉下拉功能
    [self addHeader];
    [self addFooter];

    
    
    [_allStoryCollectionView release];
    [_flowLayout release];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"allStory" forIndexPath:indexPath];
    ZSY_AllStoryModel *allStory = [_allStoryArray objectAtIndex:indexPath.row];
    self.cell.allStory = allStory;
    return _cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _allStoryArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ZSY_HomeDetailViewController *homeDetailVC = [[ZSY_HomeDetailViewController alloc] init];
    ZSY_AllStoryModel *model = [_allStoryArray objectAtIndex:indexPath.row];
    homeDetailVC.share_url = model.share_url;
    [self.navigationController pushViewController:homeDetailVC animated:YES];
    [homeDetailVC release];
}

// 左上角点击实现
- (void)leftButtonAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark --下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    
    __block ZSY_AllStoryListViewController *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量
    
    // 添加下拉刷新头部控件
    [self.allStoryCollectionView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.nextPage = 0;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        [vc getDataList:vc.nextPage];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.allStoryCollectionView headerBeginRefreshing];
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block ZSY_AllStoryListViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.allStoryCollectionView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.nextPage += 12;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc getDataList:vc.nextPage];//请求数据
        
    }];
}
#pragma 用AFN请求数据
- (void)getDataList:(NSInteger)next {
    [ZSY_AppTools creatingDataTask:[NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%ld", next] parameters:nil Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        NSArray *hotArray = [dataDic objectForKey:@"hot_spot_list"];
        for (NSDictionary *spot in hotArray) {
            ZSY_AllStoryModel *allStory = [[ZSY_AllStoryModel alloc] init];
            [allStory setValuesForKeysWithDictionary:spot];
            [self.allStoryArray addObject:allStory];
            [allStory release];
        }
        
        [self.allStoryCollectionView footerEndRefreshing];
        [self.allStoryCollectionView headerEndRefreshing];
        [self.allStoryCollectionView reloadData];
    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
